package xml;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

/**
 * @Author wangzhipeng
 * @Date 2020-08-25 8:23 下午
 * @Description Xpath
 */
public class SysConfigParser {
    public static void main(String[] args) throws DocumentException {
        //创建解析器
        SAXReader reader = new SAXReader();
        //通过解析器的read方法将配置文件读取到内存中，生成一个Documente[org.demo4j]对象树
        Document document = reader.read("src/main/java/xml/sys-config.xml");
        //dirver-name节点元素的路径 config——>database-info-->driver-name
        //dirver-name节点元素的xpath路径 /config/database-info/driver-name
        Node node = document.selectSingleNode("/config/database-info/driver-name");
        Element driverNameElt = (Element)node;
        //获取driverNameElt节点元素u对象的文本内容
        String driverName = driverNameElt.getStringValue();
        System.out.println(driverName);//输出com.mysql.jdbc.Driver

        //url节点元素的路径 config -》 database-info —》 url
        //url节点元素的xpath路径  /config/database-info/url
        //url节点元素的xpath路径  config//url
        //url节点元素的xpath路径  //url
        Element urlEit = (Element) document.selectSingleNode("config//url");
        String url = urlEit.getStringValue();
        System.out.println(url);

        //user的xpath路径：/config/database-info/user
        //     /config/database-info/user
        //      config//user
        //      //user
        Element userEit = (Element) document.selectObject("//user");
        String user = userEit.getText();
        System.out.println(user);

        Element passwordElt = (Element) document.selectSingleNode("//password");
        String password = passwordElt.getTextTrim();
        System.out.println(password);

    }
}
